package com.wang.mandkey.tool;

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;

import com.wang.mandkey.bean.AbstracAction;
import com.wang.mandkey.bean.EventStringSet;
import com.wang.mandkey.bean.KeyBoardAction;
import com.wang.mandkey.bean.MouseAction;
import com.wang.mandkey.bean.MouseWheelAction;

public class ScriptRunner {

	public static Robot robot = null;
	static {
		try {
			robot = new Robot();
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}

	public static void execAction(String utfCode) {

		if (utfCode.startsWith(EventStringSet.CLICK)) {
			// 点击
			System.out.println("单击:" + utfCode);

			AbstracAction absAction = new MouseAction(utfCode);
			robot.mousePress(InputEvent.BUTTON1_MASK);
			robot.mouseRelease(InputEvent.BUTTON1_MASK);

			/*
			 * if(absAction.getActionName().equals(EventStringSet.MOUSE_DOWN)){
			 * robot.mousePress(InputEvent.BUTTON1_MASK); }else
			 * if(absAction.getActionName().equals(EventStringSet.MOUSE_DOWN)){
			 * robot.mouseRelease(InputEvent.BUTTON1_MASK); }
			 */

		} else if (utfCode.startsWith(EventStringSet.DOUBLE_CLICK)) {
			// 双击
			System.out.println("双击:" + utfCode);
		} else if (utfCode.startsWith(EventStringSet.RIGHT_CLICK)) {
			// 右击
			System.out.println("右击:" + utfCode);
			robot.mousePress(InputEvent.BUTTON3_MASK);
			robot.mouseRelease(InputEvent.BUTTON3_MASK);

		} else if (utfCode.startsWith(EventStringSet.KEY_DOWN)) {
			// 按下键
			System.out.println("按下键:" + utfCode);
			KeyBoardAction aciton = new KeyBoardAction(utfCode);
			int keydown_code = aciton.getActionCode();
			robot.keyPress((keydown_code));
			// 传送过来的数据过为：
		} else if (utfCode.startsWith(EventStringSet.KEY_UP)) {
			// 松开键
			KeyBoardAction aciton = new KeyBoardAction(utfCode);
			robot.keyRelease((int) aciton.getActionCode());
			System.out.println("松开键:" + utfCode);

		} else if (utfCode.startsWith(EventStringSet.MOVE_MOUSE)) {
			// 移动鼠标
			System.out.println("移动鼠标:" + utfCode);
			
			try{
			MouseAction mouseAction = new MouseAction(utfCode);
			robot.mouseMove(mouseAction.getActionX(), mouseAction.getActionY());
			}catch (Exception e) {
				e.printStackTrace();
			}

		} else if (utfCode.startsWith(EventStringSet.MOUSE_DOWN)) {
			// 鼠标按下
			System.out.println("鼠标按下:" + utfCode);
			MouseAction mouseAction = new MouseAction(utfCode);
			robot.mousePress(InputEvent.BUTTON1_MASK);

		} else if (utfCode.startsWith(EventStringSet.MOUSE_UP)) {
			// 鼠标抬起
			System.out.println("鼠标抬起 :" + utfCode);
			MouseAction mouseAction = new MouseAction(utfCode);
			robot.mouseRelease(InputEvent.BUTTON1_MASK);

		} else if (utfCode.startsWith(EventStringSet.MOVE_ADD)) {
			Point point = MouseInfo.getPointerInfo().getLocation();
			MouseAction mouseAction = new MouseAction(utfCode);
			if (null != point) {
				robot.mouseMove(point.x + mouseAction.getActionX(), point.y
						+ mouseAction.getActionY());
			}
		} else if (utfCode.startsWith(EventStringSet.MOUSE_WHEEL)) {
			MouseWheelAction mwheel = new MouseWheelAction(utfCode);
			robot.mouseWheel(mwheel.getDeltaY());
		}

	}

}
